console.clear();
world.addCollisionFilter('player', 'player');
import { information } from "./ui";
import { loadPlayer,leaderBoard,savePlayer,initPlayer,getPlayerData,deletePlayer,deleteAllData } from "./SQL";
import { openBag,find_things_informations_by_id,bag_show_list } from "./bag";
import { useZswq } from "./attack";
import { jl3D,jl2D } from "./jl";
information();

function sjexp(level:number){
    return level*100+1000*(Math.floor(level/10))
}


world.onPlayerJoin(async ({ entity }) => { // 玩家进入地图后
    await loadPlayer(entity); // 读档
});

world.onPlayerLeave(async ({ entity }) => { // 玩家离开地图后
    entity.save != false ? await savePlayer(entity) : null; // 存档
})



export const things={
    1:{id:1,name:'木剑',intro:'一把木剑',type:'wq',atk:5,fatk:0,Mustlevel:0},
    2:{id:2,name:'铜剑',intro:'一把铜剑',type:'wq',atk:15,fatk:5,Mustlevel:5},
    3:{id:3,name:'极速凌云',intro:'',type:'wq',atk:30,fatk:0,Mustlevel:10},
    4:{id:4,name:'原始之刃',intro:'开服专属武器',type:'wq',atk:25,fatk:25,Mustlevel:5},
}


world.onPlayerJoin(({entity})=>{
    entity.add = function(item) {
        // 实现方法需要通过其他方式（如函数赋值）
        let new_create = true;
        entity.bagList.forEach(x=>{
            if (x.id === item.id){
                x.number+=item.number;
                new_create = false;
            }
        })
        if (new_create){
            entity.bagList.push(item);
        }
    }
    entity.delete = function(item) {
        const index = entity.bagList.findIndex(x => x.id === item.id);

        entity.bagList[index].number-=item.number;
        if (entity.bagList[index].number === 0) {
            entity.bagList.splice(index, 1);  // 正确删除空槽位
        }
    }
})//玩家add与delete实现

world.onPlayerJoin(({entity})=>{
    entity.canAttack = true;
    entity.enableDamage = true;
})






world.onPress(async({entity,button,raycast})=>{
    if (button === GameButtonType.ACTION1){
        openBag(entity);
    }else if (button === GameButtonType.ACTION0){
        useZswq(entity,entity.zswq,raycast,{
            yaw: entity.player.cameraYaw,
            pitch: entity.player.cameraPitch,
        });
    }
})


world.querySelectorAll('.canhurt').forEach(x=>{
    x.enableDamage = true;
    x.hurtList = [];
    x.takeDamage = function(number:number,attacker:GamePlayerEntity) {
        x.hurt(number);
        let new_create = true;
        x.hurtList.forEach(e=>{
            if (e.entity === attacker){
                e.number += number;
                new_create = false;
            }
        })
        const dx ={
            entity:attacker,
            number:number,
        }
        if (new_create){
            x.hurtList.push(dx);
        }
    }
})

const jr = world.querySelector('#假人');
jr.hp = jr.maxHp = 100
jr.onDie(()=>{
    jr.hp=jr.maxHp;
    jr.hurtList.forEach((e)=>{
        e.entity.exp += 10;
        e.entity.player.directMessage(`你击杀了假人,经验+10(${e.entity.exp}/${e.entity.level*100+1000*(Math.floor(e.entity.level/10))})`);
    })
    jr.hurtList=[];//清空列表

})















